package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.dao.Xjb;
import com.spider.gp.dao.XjbDto;
import com.spider.gp.dao.ZhiShu;
import com.spider.gp.util.JsonUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Json;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashMap;
import java.util.List;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class KuaiLeProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
    @Autowired
    private XjbDto xjbDto;

    @Override
    public void process(Page page) {
        List<String> all = page.getJson().jsonPath("$.data").all();

        if (all != null && all.size() > 0) {
            for (int i = 0; i < all.size(); i++) {//Selectable node : nodes 取 第一个跟最后一个
                HashMap hashMap = JSON.parseObject(all.get(i), HashMap.class);
                String frontWinningNum = hashMap.get("frontWinningNum").toString();
                String openTime = hashMap.get("openTime").toString();
                String week = hashMap.get("week").toString();
                LambdaQueryWrapper<Xjb> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Xjb::getReportTime, openTime);
                Xjb xjb = xjbDto.selectOne(wrapper);


                if (xjb != null ){
                    String winningNumber = xjb.getWinningNumber();
                    String content = xjb.getContent();
                    if (Strings.isNotBlank(winningNumber)){
                        continue;
                    }
                    if (Strings.isBlank(content)){
                        continue;
                    }

                    //开始
                    Integer[][] parse = JSON.parseObject(content, Integer[][].class);
                    StringBuffer  zjgsStr = new StringBuffer();
                    StringBuffer  zjPriceStr = new StringBuffer();

                    for (int m = 0; m < parse.length; m++) {
                        int zjgs = 0; //中奖个数
                        int price = 0; //中奖个数

                        for (int n = 0; n < parse[m].length; n++) {
                            Integer value = parse[m][n];
                            String valueStr = value.toString();
                            if (value <10){
                                valueStr = "0" + valueStr;
                            }
                            if (frontWinningNum.contains(valueStr)){
                                zjgs ++;
                            }
                        }
                        if (zjgs == 0){
                            price = 2;
                        }
                        if (zjgs == 4){
                            price = 3;
                        }
                        if (zjgs == 5){
                            price = 5;
                        }
                        if (zjgs == 6){
                            price = 20;
                        }
                        if (zjgs == 7){
                            price = 200;
                        }
                        if (zjgs == 8){
                            price = 2000;
                        }
                        if (zjgs == 9){
                            price = 300000;
                        }
                        zjgsStr.append(zjgs).append("|");
                        zjPriceStr.append(price).append("|");
                    }

                    xjb.setWeek(week);
                    xjb.setWinningPrice(zjPriceStr.toString());
                    xjb.setWinningNumber(zjgsStr.toString());
                    xjb.setPrizeNumber(frontWinningNum);
                    xjbDto.updateById(xjb);
                }
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}








